package com.ldoublem.loadingviewlib.view;

import com.ldoublem.loadingviewlib.view.base.LVBase;
import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by lumingmin on 16/6/20.
 */

public class LVCircularJump extends LVBase implements Component.DrawTask {

    private Paint mPaint;
    private float mWidth = 0f;
    private float mHight = 0f;
    private float mMaxRadius = 6;
    private int circularCount = 4;
    private float mAnimatedValue = 0f;
    private int mJumpValue = 0;

    public LVCircularJump(Context context) {
        this(context, null);
    }

    public LVCircularJump(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public LVCircularJump(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    protected void InitPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setColor(Color.WHITE);
        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        mWidth = getWidth();
        mHight = getHeight();
        float circularX = mWidth / circularCount;
        for (int i = 0; i < circularCount; i++) {
            if (i == mJumpValue % circularCount) {
                canvas.drawCircle(i * circularX + circularX / 2f,
                        mHight / 2 - mHight / 2 * mAnimatedValue,
                        mMaxRadius, mPaint);
            } else {
                canvas.drawCircle(i * circularX + circularX / 2f,
                        mHight / 2,
                        mMaxRadius, mPaint);
            }
        }
    }

    public void setViewColor(int color) {
        mPaint.setColor(new Color(color));
        invalidate();
    }

    @Override
    protected void OnAnimationRepeat(Animator animation) {
        mJumpValue++;
    }

    @Override
    protected void OnAnimationUpdate(float value) {
        mAnimatedValue = value;
        if (mAnimatedValue > 0.5) {
            mAnimatedValue = 1 - mAnimatedValue;
        }
        invalidate();
    }

    @Override
    protected int OnStopAnim() {
        mAnimatedValue = 0f;
        mJumpValue = 0;
        return 0;
    }

    @Override
    protected int SetAnimRepeatMode() {
        return RESTART;
    }

    @Override
    protected void AinmIsRunning() {

    }

    @Override
    protected int SetAnimRepeatCount() {
        return Integer.MAX_VALUE;
    }

}
